package com.tek.ding.faceding.widget;

import android.annotation.SuppressLint;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;

import androidx.core.widget.NestedScrollView;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.tek.ding.faceding.R;
import com.tek.ding.faceding.bean.Store;
import com.tek.ding.faceding.utils.DensityUtil;
import com.tek.ding.faceding.widget.adapter.StoreAdapter;

import java.util.ArrayList;
import java.util.List;

/**
 * app单点登录框
 */
@SuppressLint("CheckResult")
public class StoreDialog extends Dialog  {

    public StoreDialog(Context context) {
        super(context);
        // TODO Auto-generated constructor stub
    }

    public StoreDialog(Context context, int theme) {
        super(context, theme);
    }

    public static class Builder implements StoreAdapter.OnItemClickListener{
        private Context context;
        private StoreDialog dialog;
        private OnClickListener cancelButtonClickListener;
        private RecyclerView recyclerView;
        private List<Store> list = new ArrayList<>();
        private int position = -1, power;
        private Store data = null;

        public Builder(Context context) {
            this.context = context;
        }

        public Builder setPosition(int position) {
            this.position = position;
            return this;
        }

        public Builder setPower(int power) {
            this.power = power;
            return this;
        }

        public Builder setList(List<Store> list) {
            this.list = list;
            return this;
        }

        public Builder setCancelButton(OnClickListener listener) {
            this.cancelButtonClickListener = listener;
            return this;
        }

        public StoreDialog getDialog() {
            return dialog;
        }

        public Store getData() {
            return data;
        }

        public int getPosition() {
            return position;
        }

        public StoreDialog create() {
            LayoutInflater inflater = (LayoutInflater) context
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            // instantiate the dialog with the custom Theme
            dialog = new StoreDialog(context, R.style.CustomerDialog);
            View layout = inflater.inflate(R.layout.dialog_store, null);
//			dialog.addContentView(layout, new ViewGroup.LayoutParams(
//					ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));
            dialog.setContentView(layout);
            ViewGroup.LayoutParams layoutParams = layout.getLayoutParams();
            layoutParams.width = context.getResources().getDisplayMetrics().widthPixels;
            layout.setLayoutParams(layoutParams);
            dialog.getWindow().setGravity(Gravity.CENTER);
            dialog.setCanceledOnTouchOutside(true);// 设置点击屏幕Dialog不消失
//			dialog.setCancelable(false);
//            dialog.getWindow().setWindowAnimations(R.style.BottomDialog_Animation);

            RelativeLayout relativeLayout = layout.findViewById(R.id.rlRecy);
            NestedScrollView scrollView = layout.findViewById(R.id.scrollView);
            recyclerView = layout.findViewById(R.id.recyclerView);
            recyclerView.setLayoutManager(new LinearLayoutManager(context));

            //高度最多显示5个item的高度，如果多于5个条目，让其可滑动显示。如果少于三个条目，则自适应高度
            if (list != null && list.size() > 8) {
                resize(relativeLayout, scrollView);
            }

            StoreAdapter adapter = new StoreAdapter(context, recyclerView, list);
            recyclerView.setAdapter(adapter);
            adapter.setOnItemClickListener(this);

            return dialog;
        }

        void resize(RelativeLayout relativeLayout, NestedScrollView scrollView) {
            scrollView.setScrollbarFadingEnabled(false);
            ViewGroup.LayoutParams linearParams = relativeLayout.getLayoutParams();
            linearParams.height = DensityUtil.dip2px(50 * 8);//超高5个就固定5个item的高度
            relativeLayout.setLayoutParams(linearParams);
        }

        @Override
        public void onItemClickListener(Store data) {
            this.data = data;
            if (cancelButtonClickListener != null) {
                cancelButtonClickListener.onClick(dialog, DialogInterface.BUTTON_NEGATIVE);
            }
        }
    }

}
